//
//  WubiLesson1ViewController.m
//  Fozua
//
//  Created by sunmingming on 14-3-28.
//  Copyright (c) 2014年 EbolaPro. All rights reserved.
//

#import "WubiLessonViewController.h"
#import <FTCoreTextView.h>
#import <IDMPhoto.h>
#import <IDMPhotoBrowser.h>

@interface WubiLessonViewController ()<FTCoreTextViewDelegate,IDMPhotoBrowserDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) FTCoreTextView *coreTextView;
@property (nonatomic, assign) int lessonIndex;

@end

@implementation WubiLessonViewController

-(id)initWithLessonIndex:(int)aIndex
{
	self = [super init];
	if (self) {
		self.lessonIndex = aIndex;
		CGRect bounds = self.view.frame;
		
		UIImageView *bgView = [[UIImageView alloc] initWithFrame:bounds];
		[bgView setImage:[UIImage imageNamed:@"wubiMainBG"]];
		[self.view addSubview:bgView];
		
		self.scrollView = [[UIScrollView alloc] initWithFrame:bounds];
		self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
		
		//  Create FTCoreTextView. Everything will be rendered within this view
		self.coreTextView = [[FTCoreTextView alloc] initWithFrame:CGRectMake(bounds.origin.x + 5, bounds.origin.y, bounds.size.width - 10, bounds.size.height)];
		//        self.coreTextView = [[FTCoreTextView alloc] initWithFrame:bounds];
		self.coreTextView.backgroundColor = [UIColor clearColor];
		self.coreTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
		
		[self.coreTextView addStyles:[self coreTextStyle]];
		self.coreTextView.text = [self textForView];
		self.coreTextView.delegate = self;
		
		[self.scrollView addSubview:self.coreTextView];
		[self.view addSubview:self.scrollView];
		self.navigationItem.leftBarButtonItem =
		[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrow_white"]
																		 style:UIBarButtonItemStyleDone
																		target:self
																		action:@selector(backPressed:)];
		
		self.navigationItem.rightBarButtonItem =
		[[UIBarButtonItem alloc] initWithTitle:@"学习完毕" style:UIBarButtonItemStyleDone target:self action:@selector(nextPressed:)];
	}
	return self;
}

-(void)backPressed:(id)sender
{
	[self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
	[super viewDidLoad];
}

- (void)viewDidLayoutSubviews
{
	[super viewDidLayoutSubviews];
	[self.coreTextView fitToSuggestedHeight];
	CGFloat cha = self.scrollView.frame.size.height > CGRectGetMaxY(self.coreTextView.frame)?  (self.scrollView.frame.size.height - CGRectGetMaxY(self.coreTextView.frame) + 20.0f):40.0f;
	[self.scrollView setContentSize:CGSizeMake(CGRectGetWidth(self.scrollView.bounds), CGRectGetMaxY(self.coreTextView.frame)+cha)];
}

#pragma mark Load Static Content

- (NSString *)textForView
{
	return [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"wubilesson%d",self.lessonIndex] ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
}

-(void)nextPressed:(id)sender
{
	NSLog(@"nextPressed");
	if([APPALL.mydefaults integerForKey:@"nowLevel"] <= self.lessonIndex)
	{
		[APPALL.mydefaults setInteger:([APPALL.mydefaults integerForKey:@"nowLevel"]+1) forKey:@"nowLevel"];
	}
	[self.navigationController popViewControllerAnimated:YES];
}

#pragma mark Styling

- (NSArray *)coreTextStyle
{
	NSMutableArray *result = [NSMutableArray array];
	
	//  This will be default style of the text not closed in any tag
	FTCoreTextStyle *defaultStyle = [FTCoreTextStyle new];
	defaultStyle.name = FTCoreTextTagDefault;	//thought the default name is already set to FTCoreTextTagDefault
	defaultStyle.font = [UIFont systemFontOfSize:16.f];
	defaultStyle.textAlignment = FTCoreTextAlignementJustified;
	[result addObject:defaultStyle];
	
	//  Create style using convenience method
	FTCoreTextStyle *titleStyle = [FTCoreTextStyle styleWithName:@"title"];
	titleStyle.font = [UIFont systemFontOfSize:40.f];
	titleStyle.paragraphInset = UIEdgeInsetsMake(20.f, 0, 25.f, 0);
	titleStyle.textAlignment = FTCoreTextAlignementCenter;
	[result addObject:titleStyle];
	
	//  Image will be centered
	FTCoreTextStyle *imageStyle = [FTCoreTextStyle new];
	imageStyle.name = FTCoreTextTagImage;
	imageStyle.textAlignment = FTCoreTextAlignementCenter;
	[result addObject:imageStyle];
	
	FTCoreTextStyle *firstLetterStyle = [FTCoreTextStyle new];
	firstLetterStyle.name = @"firstLetter";
	firstLetterStyle.font = [UIFont boldSystemFontOfSize:30.f];
	[result addObject:firstLetterStyle];
	
	//  This is the link style
	//  Notice that you can make copy of FTCoreTextStyle
	//  and just change any required properties
	FTCoreTextStyle *linkStyle = [defaultStyle copy];
	linkStyle.font = [UIFont systemFontOfSize:14.f];
	linkStyle.name = FTCoreTextTagLink;
	linkStyle.color = [UIColor blueColor];
	linkStyle.textAlignment = FTCoreTextAlignementCenter;
	[result addObject:linkStyle];
	
	FTCoreTextStyle *subtitleStyle = [FTCoreTextStyle styleWithName:@"subtitle"];
	subtitleStyle.font = [UIFont boldSystemFontOfSize:25.f];
	subtitleStyle.color = [UIColor brownColor];
	subtitleStyle.paragraphInset = UIEdgeInsetsMake(10, 0, 10, 0);
	[result addObject:subtitleStyle];
	
	//  This will be list of items
	//  You can specify custom style for a bullet
	FTCoreTextStyle *bulletStyle = [defaultStyle copy];
	bulletStyle.name = FTCoreTextTagBullet;
	bulletStyle.bulletFont = [UIFont systemFontOfSize:16.f];
	bulletStyle.bulletColor = [UIColor orangeColor];
	bulletStyle.bulletCharacter = @"▶";
	bulletStyle.paragraphInset = UIEdgeInsetsMake(0, 20.f, 0, 0);
	[result addObject:bulletStyle];
	
	FTCoreTextStyle *italicStyle = [defaultStyle copy];
	italicStyle.name = @"italic";
	italicStyle.underlined = YES;
	italicStyle.font = [UIFont italicSystemFontOfSize:16.f];
	[result addObject:italicStyle];
	
	FTCoreTextStyle *boldStyle = [defaultStyle copy];
	boldStyle.name = @"bold";
	boldStyle.font = [UIFont boldSystemFontOfSize:16.f];
	[result addObject:boldStyle];
	
	FTCoreTextStyle *coloredStyle = [defaultStyle copy];
	[coloredStyle setName:@"colored"];
	[coloredStyle setColor:[UIColor redColor]];
	[result addObject:coloredStyle];
	
	return  result;
}

#pragma mark FTCoreTextViewDelegate

- (void)coreTextView:(FTCoreTextView *)acoreTextView receivedTouchOnData:(NSDictionary *)data
{
	NSURL *url = [data objectForKey:FTCoreTextDataURL];
	NSLog(@"join us!!%@",[url absoluteString]);
	if([[url absoluteString] hasPrefix:@"http://"])
	{
		//[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"wubilesson%d",self.lessonIndex] ofType:@"txt"]
		NSString *want = [[url absoluteString] substringFromIndex:7];
		NSString *pathStr =  [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"lesson%@@2x",want] ofType:@"png"];
		NSLog(@"pathr:%@    %@",pathStr,want);
		IDMPhoto *photo = [IDMPhoto photoWithFilePath:pathStr];
		photo.caption = @"";
		NSArray *photos = @[photo];
		IDMPhotoBrowser *browser = [[IDMPhotoBrowser alloc] initWithPhotos:photos animatedFromView:self.view]; // using
		browser.delegate = self;
		browser.displayActionButton = NO;
		browser.displayArrowButton = NO;
		browser.displayCounterLabel = NO;
		browser.usePopAnimation = NO;
		browser.scaleImage = nil;
		browser.useWhiteBackgroundColor = NO;
		[self presentViewController:browser animated:YES completion:nil];
	}
}

#pragma mark - IDMPhotoBrowser Delegate

- (void)photoBrowser:(IDMPhotoBrowser *)photoBrowser didShowPhotoAtIndex:(NSUInteger)pageIndex
{
	/*
	 id <IDMPhoto> photo = [photoBrowser photoAtIndex:pageIndex];
	 NSLog(@"Did show photoBrowser with photo index: %zu, photo caption: %@", pageIndex, photo.caption);
	 */
}

- (void)photoBrowser:(IDMPhotoBrowser *)photoBrowser willDismissAtPageIndex:(NSUInteger)pageIndex
{
	/*
	 id <IDMPhoto> photo = [photoBrowser photoAtIndex:pageIndex];
	 NSLog(@"Will dismiss photoBrowser with photo index: %zu, photo caption: %@", pageIndex, photo.caption);
	 */
}

- (void)photoBrowser:(IDMPhotoBrowser *)photoBrowser didDismissAtPageIndex:(NSUInteger)pageIndex
{
	/*
	 id <IDMPhoto> photo = [photoBrowser photoAtIndex:pageIndex];
	 NSLog(@"Did dismiss photoBrowser with photo index: %zu, photo caption: %@", pageIndex, photo.caption);
	 */
}

- (void)photoBrowser:(IDMPhotoBrowser *)photoBrowser didDismissActionSheetWithButtonIndex:(NSUInteger)buttonIndex photoIndex:(NSUInteger)photoIndex
{
	/*
	 id <IDMPhoto> photo = [photoBrowser photoAtIndex:photoIndex];
	 NSLog(@"Did dismiss actionSheet with photo index: %zu, photo caption: %@", photoIndex, photo.caption);
	 
	 NSString *title = [NSString stringWithFormat:@"Option %zu", buttonIndex+1];
	 [UIAlertView showAlertViewWithTitle:title];
	 */
}

@end
